#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const long long mod = 998244353;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
long long qPow(long long base, long long n) {
  long long res = 1;
  while (n) {
    if ((n & 1)) res = (res * base) % mod;
    base = (base * base) % mod;
    n >>= 1;
  }
  return res;
}
long long n;
int main() {
  while (~scanf("%lld", &n)) {
    long long ans = (qPow(3, n * n) - qPow((qPow(3, n) - 3), n) + mod) % mod;
    int f = -1;
    long long t = 0, c1 = 1;
    for (int i = 1; i <= n; i++) {
      f *= -1;
      c1 = (c1 * (n - (i - 1))) % mod;
      c1 = (c1 * qPow(i, mod - 2)) % mod;
      ans += (f * c1 % mod *
              (3 * qPow(qPow(3, n - i) - 1, n) % mod +
               (qPow(3, i) - 3) % mod * qPow(3, (n - i) % mod * n) % mod) %
              mod) %
             mod;
    }
    ans = (ans % mod + mod) % mod;
    printf("%lld\n", ans);
  }
  return 0;
}
