#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const double oula = 0.57721566490153286060651209;
using namespace std;
long long fac[1000006], inv[1000006];
long long ksm(long long a, long long b) {
  long long ans = 1;
  a %= MOD;
  while (b) {
    if (b & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
long long cal(long long x, long long y) {
  return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
int main() {
  std::ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long ans = 0;
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = ksm(fac[i], MOD - 2);
  }
  for (int i = 1; i <= n; i++) {
    ans = (ans + ksm(3, (n * (n - i) + i)) * ksm(-1, i + 1) * cal(n, i) % MOD +
           MOD) %
          MOD;
  }
  ans = ans * 2 % MOD;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    long long t = -ksm(3, i);
    tmp =
        (tmp +
         cal(n, i) * ksm(-1, i + 1) * (ksm(1 + t, n) - ksm(t, n) + MOD) % MOD +
         MOD) %
        MOD;
  }
  ans = (ans + tmp * 3) % MOD;
  cout << ans << endl;
}
