#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long qpow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret = ret * x % MOD;
    y >>= 1;
    x = x * x % MOD;
  }
  return ret % MOD;
}
int main() {
  long long n, sum = 0, C = 1;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    C = C * (n - i + 1) % MOD * qpow(i, MOD - 2) % MOD;
    if (i & 1)
      sum = (sum + C * qpow(3, i) % MOD * qpow(3, n * (n - i))) % MOD;
    else
      sum = ((sum - C * qpow(3, i) % MOD * qpow(3, n * (n - i))) % MOD + MOD) %
            MOD;
  }
  sum = sum * 2 % MOD;
  C = 1;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (i) C = C * (n - i + 1) % MOD * qpow(i, MOD - 2) % MOD;
    long long tmp = qpow(((1 - qpow(3, i)) % MOD + MOD) % MOD, n);
    if (n & 1)
      tmp = (tmp + qpow(3, i * n)) % MOD;
    else
      tmp = (tmp - qpow(3, i * n) + MOD) % MOD;
    if (i & 1)
      ans = (ans + tmp * C) % MOD;
    else
      ans = ((ans - tmp * C) % MOD + MOD) % MOD;
  }
  cout << (sum + ans * 3) % MOD << endl;
  return 0;
}
